<template>
  <div class="demo" :class="{[`${classPrefix}-demo`]: classPrefix}">
    <h3>{{ $t(component.__i18nTitle) }}</h3>
    <div class="demo-component">
      <component :is="component" />
    </div>
    <div class="demo-actions">
      <Button no-ripple level="secondary"
        @click="showCode = !showCode">
        {{ showCode ? $t('demos.button.hideCode')
        : $t('demos.button.checkCode') }}
      </Button>
    </div>
    <div class="demo-code" v-if="showCode">
      <pre class="language-html"
        v-html="Prism.highlight(component.__sourceCode,
        Prism.languages.html, 'html')" />
    </div>
  </div>
</template>

<script lang="ts">
import { ref } from 'vue'
import 'prismjs'
import 'prismjs/themes/prism.css'
import { Button } from 'neat-ui-dev'

const Prism = window.Prism
export default {
  components: {
    Button
  },
  props: {
    component: Object,
    classPrefix: String
  },
  setup() {
    const showCode = ref<Boolean>(false)
    return { Prism, showCode }
  }
}
</script>

<style lang="scss" scoped>
.demo {
  border: 1px solid #E0E0E0;
  margin: 16px 0 32px;
  > h3 {
    font-size: 20px;
    padding: 8px 16px;
    border-bottom: 1px solid #E0E0E0;
  }
  &-component {
    padding: 16px;
  }
  &-actions {
    padding: 8px 16px;
    border-top: 1px dashed #BDBDBD;
  }
  &-code {
    padding: 8px 16px;
    border-top: 1px dashed #E0E0E0;
    > pre {
      line-height: 1.1;
      font-family: Consolas, 'Courier New', Courier, monospace;
      margin: 0;
    }
  }
}
.language-html {
  background: #fff;
}
.demo-actions {
  button.neat-button {
    width: 116px;
  }
}
</style>